package com.redxiii.tracplus.ejb.util;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redxiii.tracplus.ejb.entity.TicketAccelerator;
import com.thoughtworks.xstream.XStream;

/**
 * Ticket Accelerator Creation - Configuration
 * @author dfilgueiras
 *
 */
public class TicketAccConfig {

	private static final Logger logger = LoggerFactory.getLogger(TicketAccConfig.class);
	
	private final List<TicketAccelerator> accelerators = new ArrayList<TicketAccelerator>();
	private static TicketAccConfig instance;
	
	private static final String url = "tracplus-ticket-acc.xml";
	
	
	private TicketAccConfig() {
//		String url = System.getProperty("jboss.server.config.url") + "/tracplus-ticket-acc.xml";
		
		logger.debug("Loading Configuration file '{}'", url);
		try {
			
			accelerators.clear();
			
			XStream stream = new XStream();
			List<TicketAccelerator> accelerators = (List<TicketAccelerator>) stream.fromXML(url);
			
			if (accelerators != null) {
				this.accelerators.addAll(accelerators);
			}
			
			logger.debug("Configuration file '{}' loaded", url);
		} catch (Exception e) {
			logger.error("Unable to load configuration file {}", e, url);
		} 
	}
	
	public synchronized static TicketAccConfig getInstance() {
		if (instance == null)
			instance = new TicketAccConfig();
		
		return instance;
	}
	
	public List<TicketAccelerator> getAccelerators() {
		return accelerators;
	}
}
